package model.entity;

import game_engine.GameEngine;
import game_engine.core.Entity;
import game_engine.core.EntityGroup;
import game_engine.core.Vector2f;
import game_engine.display.GEUtil;
import game_engine.display.Texture;
import game_engine.display.TextureLoader;
import game_engine.test.GameTest;

import java.awt.Color;
import java.awt.Rectangle;
import java.io.IOException;

public class Fighter extends Entity {

	private int id;
	public final static int FRAME_NOMOVE = 0;
	public final static int FRAME_MOVING_LEFT = 1;
	public final static int FRAME_MOVING_RIGHT = 2;
	public final static int FRAME_FIRING = 3;
	public final static int FRAME_DYING = 4;

	private boolean wShooting = false;
	private boolean xShooting = false;

	private long actionTick = 100;
	private long lastActionTick = 0;
	
	private final long deathMessageLength = 1000;
	private boolean startDeathMessage = false;
	private long currentDeathMessage = 0;
	
	private static final Color transColor = new Color(0xbfdcbf);
	
	private EntityGroup bullets; 

	public Fighter(int id, String name) {
		super(name);
		this.id = id;
		Texture[][] frames = new Texture[3][];
		GameEngine ge = GameEngine.getInstance();
		Texture[] noMove = new Texture[] { ge.getTexture("images/fighter/fighter_mid.png", transColor) };
		Texture[] move_left = new Texture[] { ge.getTexture("images/fighter/fighter_left2.png", transColor),
				ge.getTexture("images/fighter/fighter_left1.png", transColor) };
		Texture[] move_right = new Texture[] { ge.getTexture("images/fighter/fighter_right2.png", transColor),
				ge.getTexture("images/fighter/fighter_right1.png", transColor) };
		frames[FRAME_NOMOVE] = noMove;
		frames[FRAME_MOVING_LEFT] = move_left;
		frames[FRAME_MOVING_RIGHT] = move_right;
		
		int[][] framesCountList = new int[3][];
		int[] framesCount1 = new int[] { 100 };
		int[] framesCount2 = new int[] { 200, 500 };
		int[] framesCount3 = new int[] { 200, 500 };
		framesCountList[FRAME_NOMOVE] = framesCount1;
		framesCountList[FRAME_MOVING_LEFT] = framesCount2;
		framesCountList[FRAME_MOVING_RIGHT] = framesCount3;

		boolean[] frameLoop = new boolean[3];
		frameLoop[FRAME_NOMOVE] = false;
		frameLoop[FRAME_MOVING_LEFT] = false;
		frameLoop[FRAME_MOVING_RIGHT] = false;
		currentFrame = 0;
		currentAction = FRAME_NOMOVE;

		boundingBox = new Rectangle(18, 18, 4, 4);

		this.setFrames(frames);
		this.setFrameCount(framesCountList);
		this.setFrameLoop(frameLoop);
		
		this.bullets = new EntityGroup();
	}

	protected void initShot() {

	}
	
	public EntityGroup getBullets() {
		return bullets;
	}
	
	@Override
	public void move(long tick) {
		super.move(tick);
		if (position.x > GameEngine.resolution.width)
			position.x = GameEngine.resolution.width;
		if (position.x < 0)
			position.x = 0;
		if (position.y > GameEngine.resolution.height)
			position.y = GameEngine.resolution.height;
		if (position.y < 0)
			position.y = 0;
	}

	@Override
	public void render() {
		if (position != null) {
			if (currentFrame < frames[currentAction].length) {
				GEUtil.drawImage(frames[currentAction][currentFrame], Math.round(position.x), Math.round(position.y), 40, 40, 255);
				//TODO draw hit box
				//GEWindow.fillRect(position.x+18, position.y+18, 4, 4, Color.red);
				//TODO Re-think death system via game phase
				/*
				if(startDeathMessage) {
					currentDeathMessage+=tick;
					float alpha = (float)currentDeathMessage/(float)deathMessageLength;
					if(alpha > 1.0f) {
						startDeathMessage = false;
						currentDeathMessage = 0;
						alpha = 1.0f;
					}
					GEWindow.drawString("GAMEOVER", GEWindow.resolution.width/2-150, GEWindow.resolution.height/2-50, 50, Color.lightGray, alpha);
				}
				GEWindow.drawString("Score : " + value, 40, 40, 15, Color.red.brighter(), 1.0f);
				*/
			}
			else
				System.out.println("currentFrame issues : " + currentFrame + ", action = " + currentAction);
		}
	}

	@Override
	public void collided(Entity e) {
		if (e instanceof Enemy || e instanceof Bullet) {
			unspawning = true;
			startDeathMessage = true;
		}
	}

	@Override
	public void doAction(float tick) {
		if (wShooting && lastActionTick > actionTick) {
			for (int i = 0; i < 3; i++) {
				Bullet bullet = new Bullet(this);
				Vector2f position1 = new Vector2f(position.x + 15*i, position.y - 5);
				bullet.setPosition(position1);
				bullet.setDirection(new Vector2f(-0.20f, -1f));
				bullet.setSpeed(1.0f);
				float[] rotations = new float[]{5f, -5f};
				bullet.setRotations(rotations);
				int[] rotationsTiming = new int[]{200, 200};
				bullet.setRotationsTiming(rotationsTiming);
				bullet.setRotationsLoop(true);
				bullet.setBox(new Rectangle(0, 0, 10, 10));
				bullets.addEntity(bullet);
				lastActionTick = 0;
			}
		} else if (xShooting && lastActionTick > actionTick) {
			for (int i = -20; i < 21; i++) {
				Bullet bullet = new Bullet(this);
				Vector2f position1 = new Vector2f(position.x + 15, position.y - 5);
				bullet.setPosition(position1);
				float[] rotations = new float[]{(float) (-Math.signum(i)*Math.sqrt(Math.abs(i))*5f), 0f};
				bullet.setRotations(rotations);
				int[] rotationsTiming = new int[]{300, 0};
				bullet.setRotationsTiming(rotationsTiming);
				bullet.setRotationsLoop(false);
				bullet.setSpeed(1.0f);
				bullet.setDirection(new Vector2f(i * 0.5f, -1f));
				bullet.setBox(new Rectangle(0, 0, 10, 10));
				bullets.addEntity(bullet);
				lastActionTick = 0;
			}
		} else
			lastActionTick += tick;
	}

	public boolean iswShooting() {
		return wShooting;
	}

	public void setwShooting(boolean wShooting) {
		this.wShooting = wShooting;
	}

	public boolean isxShooting() {
		return xShooting;
	}

	public void setxShooting(boolean xShooting) {
		this.xShooting = xShooting;
	}

	public int getId() {
		return id;
	}
}
